﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;


namespace AssetManageSystem.ViewModel.AssetManage.AssetAllocationVMs
{
    public partial class AssetAllocationListVM : BasePagedListVM<AssetAllocation_View, AssetAllocationSearcher>
    {
        protected override List<GridAction> InitGridAction()
        {
            if (SearcherMode == ListVMSearchModeEnum.MasterDetail)
            {
                return new List<GridAction>();
            }
            else
            {
                return new List<GridAction>
            {
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-green"),
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-blue"),
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-8"),
                // this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-orange"),
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                // this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-4"),
                this.MakeStandardAction("AssetAllocation", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "AssetManage").SetButtonClass("btn-col-4"),
            };
            }
        }


        protected override IEnumerable<IGridColumn<AssetAllocation_View>> InitGridHeader()
        {
            if (SearcherMode == ListVMSearchModeEnum.MasterDetail)
            {
                return new List<GridColumn<AssetAllocation_View>>{
                this.MakeGridHeader(x => x.DocNumber),
                this.MakeGridHeader(x => x.UserName_view),
                this.MakeGridHeader(x => x.DepName_view),
                this.MakeGridHeader(x => x.UserName_view2),
                this.MakeGridHeader(x => x.DepName_view2),
                this.MakeGridHeader(x => x.OutDate),
                //this.MakeGridHeader(x => x.AsName_view),
            };
            }
            else
            {
                return new List<GridColumn<AssetAllocation_View>>{
                this.MakeGridHeader(x => x.DocNumber),
                this.MakeGridHeader(x => x.UserName_view),
                this.MakeGridHeader(x => x.DepName_view),
                this.MakeGridHeader(x => x.UserName_view2),
                this.MakeGridHeader(x => x.DepName_view2),
                this.MakeGridHeader(x => x.OutDate),
                //this.MakeGridHeader(x => x.AsName_view),
                this.MakeGridHeaderAction(width: 200)
            };
            }
        }

        public override IOrderedQueryable<AssetAllocation_View> GetSearchQuery()
        {
            var query = DC.Set<AssetAllocation>()
                .CheckContain(Searcher.DocNumber, x => x.DocNumber)
                .CheckEqual(Searcher.OldUserId, x => x.OldUserId)
                .CheckEqual(Searcher.OldDepId, x => x.OldDepId)
                .CheckEqual(Searcher.NewUserId, x => x.NewUserId)
                .CheckEqual(Searcher.NewDepId, x => x.NewDepId)
                .CheckEqual(Searcher.AssetId, x => x.AssetId)
                //.CheckWhere(Searcher.SelectedAssetIdIDs, x => DC.Set<AssetAllocationMiddleTable>().Where(y => Searcher.SelectedAssetIdIDs.Contains(y.AssetId)).Select(z => z.AssetAllocationId).Contains(x.ID))
                .Select(x => new AssetAllocation_View
                {
                    ID = x.ID,
                    DocNumber = x.DocNumber,
                    UserName_view = x.OldUser.UserName,
                    DepName_view = x.OldDep.DepName,
                    UserName_view2 = x.NewUser.UserName,
                    DepName_view2 = x.NewDep.DepName,
                    OutDate = x.OutDate,
                    //AsName_view = x.AssetId.Where(y=>y.Asset.IsValid==true).Select(y=>y.Asset.AsName).ToSepratedString(null,","), 
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class AssetAllocation_View : AssetAllocation
    {
        [Display(Name = "原使用人")]
        public String UserName_view { get; set; }
        [Display(Name = "原使用部门")]
        public String DepName_view { get; set; }
        [Display(Name = "现使用人")]
        public String UserName_view2 { get; set; }
        [Display(Name = "现使用部门")]
        public String DepName_view2 { get; set; }
        [Display(Name = "资产名称")]
        public String AsName_view { get; set; }

    }
}
